import 'package:dartz/dartz.dart';
import 'package:dio/dio.dart';
import 'package:netflix_frontend/core/constants/api_url.dart';
import 'package:netflix_frontend/core/network/dio_client.dart';
import 'package:netflix_frontend/data/auth/models/signup_req_params.dto.dart';
import 'package:netflix_frontend/data/auth/models/login_req_params.dto.dart';
import 'package:netflix_frontend/service_locator.dart';

abstract class AuthService {
  Future<Either> login(LoginReqParams params);
  Future<Either> signin(SignupReqParams params);
}

class AuthApiServiceImpl extends AuthService {
  @override
  Future<Either> login(LoginReqParams params) async {
    try {
      var resp = await sl<DioClient>().post(
        ApiUrl.signup,
        data: params.toMap(),
      );

      return Right(resp.data);
    } on DioException catch (e) {
      return Left(e.response!.data['message']);
    }
  }

  @override
  Future<Either> signin(SignupReqParams params) async {
    try {
      var resp = await sl<DioClient>().post(
        ApiUrl.login,
        data: params.toMap(),
      );

      return Right(resp.data);
    } on DioException catch (e) {
      return Left(e.response!.data['message']);
    }
  }
}

// DONE 